﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace YouxinHelper.Helper.Currency
{
    class WebCookies
    {/// <summary>
     /// 带 Cookie 的 WebClient
     /// </summary>
        public class CookieWebClient : WebClient
        {
            // Cookie 容器
            public CookieContainer Cookies;

            /// <summary>
            /// 创建一个新的 CookieWebClient 实例。
            /// </summary>
            public CookieWebClient()
            {
                Console.WriteLine("cookies");
                this.Cookies = new CookieContainer();
            }

            protected override WebRequest GetWebRequest(Uri address)
            {
                WebRequest request = base.GetWebRequest(address);
                if (request is HttpWebRequest)
                {
                    HttpWebRequest httpRequest = request as HttpWebRequest;
                    Console.WriteLine("cookies赋值");
                    httpRequest.CookieContainer = Cookies;
                }
                return request;
            }
        }

      public  static WebClient RequestWeb = new CookieWebClient();
        /// <summary>
        /// 发送post
        /// </summary>
        /// <param name="regUrl"></param>
        /// <param name="PostData"></param>
        /// <returns></returns>
        public static string HttpPost(string regUrl, String PostData)
        {
            string Html = "";
            try
            {

              


                RequestWeb.Encoding = Encoding.UTF8;
                

                    RequestWeb.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                RequestWeb.Headers.Add("User-Agent", " Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0");
                RequestWeb.Headers.Add("Referer", "https://yx.357.com/");
                byte[] post = Encoding.UTF8.GetBytes(PostData);
                    byte[] data = RequestWeb.UploadData(regUrl, post);
                    Html = Encoding.UTF8.GetString(data);
                
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex);
            }
            return Html;
        }

        /// <summary>
        /// 发送post
        /// </summary>
        /// <param name="regUrl"></param>
        /// <param name="PostData"></param>
        /// <returns></returns>
        public static string HttpGet(string GetUrl)
        {
            RequestWeb.Headers.Remove("Content-Type");
            RequestWeb.Headers.Add("Referer", "https://yx.357.com/");
            string Html = "";
            try
            {

                RequestWeb.Encoding = Encoding.UTF8;
             
                    Html = new StreamReader(RequestWeb.OpenRead(GetUrl)).ReadToEnd();
              
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex);
            }
            return Html;
        }

        /// <summary>
        /// 发送post
        /// </summary>
        /// <param name="regUrl"></param>
        /// <param name="PostData"></param>
        /// <returns></returns>
        public static Stream HttpGetStream(string GetUrl)
        {
            RequestWeb.Headers.Remove("Content-Type");
            RequestWeb.Headers.Add("Referer", "https://yx.357.com/");
            Stream GetStream = null;
            try
            {

                RequestWeb.Encoding = Encoding.UTF8;
               
                   return RequestWeb.OpenRead(GetUrl);
               
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex);
            }
            return GetStream;
        }


    }
}
